package com.yi.servicemap.service;

import com.yi.internalcommon.constant.AmapConfigConstants;
import com.yi.internalcommon.constant.CommonStatusEnum;
import com.yi.internalcommon.dto.DicDistrict;
import com.yi.internalcommon.dto.ResponseResult;
import com.yi.servicemap.mapper.DicDistrictMapper;
import com.yi.servicemap.remote.MapDicDistrictClient;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class DicDistrictService {
    @Autowired
    private MapDicDistrictClient mapDicDistrictClient;
    @Autowired
    private DicDistrictMapper dicDistrictMapper;
    public ResponseResult initDicDistrict(String keywords){
        //请求地图
        String dicDistrict = mapDicDistrictClient.dicDistrict(keywords);
        System.out.println(dicDistrict);
        //解析结果
        JSONObject dicDistrictJsonObject = JSONObject.fromObject(dicDistrict);
            int status = dicDistrictJsonObject.getInt(AmapConfigConstants.STATUS);
            if(status!=1){
                return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_EMPTY.getCode(),CommonStatusEnum.MAP_DISTRICT_EMPTY.getValue());
            }
            JSONArray countryJsonArray = dicDistrictJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
            for (int country = 0; country < countryJsonArray.size(); country++) {
                JSONObject countryJsonObject = countryJsonArray.getJSONObject(country);
                String countryAddressCode=countryJsonObject.getString(AmapConfigConstants.ADCODE);
                String countryAddressName=countryJsonObject.getString(AmapConfigConstants.NAME);
                String countryParentAddressCode="0";
                String countryLevel=countryJsonObject.getString(AmapConfigConstants.LEVEL);

                insertDistrict(countryAddressCode, countryAddressName, countryParentAddressCode, countryLevel);
                JSONArray provinceJsonArray = countryJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                for (int p = 0; p < provinceJsonArray.size(); p++) {
                    JSONObject provinceJsonObject = provinceJsonArray.getJSONObject(p);
                    String provinceAddressCode=provinceJsonObject.getString(AmapConfigConstants.ADCODE);
                    String provinceAddressName=provinceJsonObject.getString(AmapConfigConstants.NAME);
                    String provinceParentAddressCode=countryAddressCode;
                    String provinceLevel=provinceJsonObject.getString(AmapConfigConstants.LEVEL);

                    insertDistrict(provinceAddressCode, provinceAddressName, provinceParentAddressCode, provinceLevel);

                    JSONArray cityJsonArray = provinceJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                    for (int city = 0; city < cityJsonArray.size(); city++) {
                        JSONObject cityJsonObject = cityJsonArray.getJSONObject(city);
                        String cityAddressCode=cityJsonObject.getString(AmapConfigConstants.ADCODE);
                        String cityAddressName=cityJsonObject.getString(AmapConfigConstants.NAME);
                        String cityParentAddressCode=provinceAddressCode;
                        String cityLevel=cityJsonObject.getString(AmapConfigConstants.LEVEL);

                        insertDistrict(cityAddressCode, cityAddressName, cityParentAddressCode, cityLevel);

                        JSONArray districtJsonArray = cityJsonObject.getJSONArray(AmapConfigConstants.DISTRICTS);
                        for (int d = 0; d < districtJsonArray.size(); d++) {
                            JSONObject districtJsonObject = districtJsonArray.getJSONObject(d);
                            String districtAddressCode=districtJsonObject.getString(AmapConfigConstants.ADCODE);
                            String districtAddressName=districtJsonObject.getString(AmapConfigConstants.NAME);
                            String districtParentAddressCode=cityAddressCode;
                            String districtLevel=districtJsonObject.getString(AmapConfigConstants.LEVEL);
                            if(districtLevel.equals(AmapConfigConstants.STREET)){
                                continue;
                            }
                            insertDistrict(districtAddressCode, districtAddressName, districtParentAddressCode, districtLevel);

                        }
                    }

                }
            }
        return ResponseResult.success("");
    }

    private void insertDistrict(String addressCode, String addressName, String parentAddressCode, String level) {
        //数据库对象
        DicDistrict district=new DicDistrict();
        district.setAddressCode(addressCode);
        district.setAddressName(addressName);
        int leveInt=generateLevel(level);
        district.setLevel(leveInt);

        district.setParentAddressCode(parentAddressCode);
        //插入数据库
        dicDistrictMapper.insert(district);
    }

    public int generateLevel(String level){
        int leveInt=0;
        if(level.equals("country")){
            leveInt=0;
        } else if (level.equals("province")) {
            leveInt=1;
        }else if (level.equals("city")) {
            leveInt=2;
        }else if (level.equals("district")) {
            leveInt=3;
        }
    return  leveInt;
}

}
